package com.axend.aerosense.common.ui.HeightPicker;

import android.content.Context;
import android.view.View;

import com.axend.aerosense.common.R;
import com.jzxiang.pickerview.adapters.NumericWheelAdapter;
import com.jzxiang.pickerview.utils.PickerContants;
import com.jzxiang.pickerview.wheel.OnWheelChangedListener;
import com.jzxiang.pickerview.wheel.WheelView;


public class HeightWheel {
    private Context mContext;

    private WheelView ftWheelView, inWheelView;
    private NumericWheelAdapter inAdapter, ftAdapter;

    private HeightConfig mHeightConfig;
    private HeightRepository mRepository;


    private OnWheelChangedListener inListener = new OnWheelChangedListener() {
        @Override
        public void onChanged(WheelView wheel, int oldValue, int newValue) {
//            updateIn();
        }
    };
    private OnWheelChangedListener ftListener = new OnWheelChangedListener() {
        @Override
        public void onChanged(WheelView wheel, int oldValue, int newValue) {
//            updateFt();
        }
    };

    public HeightWheel(View view, HeightConfig pickerConfig) {
        mHeightConfig = pickerConfig;
        mRepository = new HeightRepository(pickerConfig);
        mContext = view.getContext();
        initialize(view);
    }

    public void initialize(View view) {
        initView(view);
        initIn();
        initFt();
    }


    void initView(View view) {
        ftWheelView = (WheelView) view.findViewById(R.id.common_ft);
        inWheelView = (WheelView) view.findViewById(R.id.common_in);
        ftWheelView.addChangingListener(ftListener);
        inWheelView.addChangingListener(inListener);
    }

    void initIn() {
        int minValue = mRepository.getFtNumRepository().getMinValue();
        int maxValue = mRepository.getFtNumRepository().getMaxValue();

        ftAdapter = new NumericWheelAdapter(mContext, minValue, maxValue, PickerContants.FORMAT_0, mHeightConfig.getFtNumberConfig().mUnit);
        ftAdapter.setConfig(mHeightConfig.getFtNumberConfig());
        ftWheelView.setViewAdapter(ftAdapter);
        ftWheelView.setCurrentItem(mRepository.getFtNumRepository().getDefaultValue() - minValue);
    }

    void initFt() {
        int inchMinValue = mRepository.getInNumRepository().getMinValue();
        int inchMaxValue = mRepository.getInNumRepository().getMaxValue();

        inAdapter = new NumericWheelAdapter(mContext, inchMinValue, inchMaxValue, PickerContants.FORMAT_0, mHeightConfig.getInNumberConfig().mUnit);
        inAdapter.setConfig(mHeightConfig.getInNumberConfig());
        inWheelView.setViewAdapter(inAdapter);
        inWheelView.setCurrentItem(mRepository.getInNumRepository().getDefaultValue() - inchMinValue);
    }


    public int getCurrentFoot() {
        return ftWheelView.getCurrentItem() + mRepository.getFtNumRepository().getMinValue();
    }

    public int getCurrentInch() {
        return inWheelView.getCurrentItem() + +mRepository.getInNumRepository().getMinValue();
    }
}
